<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/taglibs.jsp"%>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="${ctx}/resources/semanticui/semantic.min.css"
	rel="stylesheet" type="text/css" />
<link href="${ctx}/resources/apps.css" rel="stylesheet" type="text/css" />
<title>手机验证</title>
</head>
<body>
	<div class="ui segment" id="n-page">

		<div class="ui segment" id="n-content">
			<form class="ui form">
				<div class="field">
					<label>手机号码</label>
					<div class="ui input">
						<input type="text" name="phone" id="phone" placeholder="手机号码"
							value="">
					</div>
				</div>
				<div class="field">
					<label>验证码</label>
					<div class="ui input">
						<input type="text" name="code" id="code" placeholder="请输入验证码"
							value=""> &nbsp;
						<div class="ui button active dog" onclick="timeout(this);" id="sendCode">发送</div>
					</div>
				</div>

			</form>
		</div>

		<div class="ui checked checkbox">
			<input type="checkbox" id="agreement" class="hidden" checked="">

			<label>同意会猎公众号 <a href="https://tiaonali.com/hunterAgreement.htm">《用户使用协议》 </a>
			</label>
		</div>

		<div class="ui segment" id="choice-buttons">
			<div class="fluid ui button" id="next">注册</div>
		</div>
		<div class="ui center aligned segment">
			真实的注册<br> 严格的审核<br> 绝对的保密<br>
		</div>

	</div>

	<script src="${ctx}/resources/jweixin-1.0.0.js"></script>

	<script src="${ctx}/resources/semanticui/jquery-2.1.4.min.js"></script>
	<script src="${ctx}/resources/semanticui/semantic.js"
		type="text/javascript"></script>
	<script type="text/javascript">
 		wx.config({
			debug : false,
			appId : "${config.appid}",
			timestamp : ${config.currenttimestamp},
			nonceStr : "${config.nonceStr}",
			signature : "${config.signatureStr}",
			jsApiList : [ 'checkJsApi', 'onMenuShareTimeline',
					'onMenuShareAppMessage', 'onMenuShareQQ',
					'onMenuShareWeibo', 'hideOptionMenu', 'closeWindow' ]
		}); 

		function timeout(dom){
			var phone = $("#phone").val();
			if (!phone || phone == "") {
				alert("您必须输入手机号码");
				return false;
			}
			var regEx = /^1[0-9]{2}[0-9]{8}$/;
			if (!phone.match(regEx)) {
				alert("您必须输入合格的手机号码");
				return false;
			}
			//设置按钮不可点击
			dom.setAttribute('disabled',true);
			//开始倒计时
		    countDown(dom); 
		    sendRegSMS();
		}
		
		function sendRegSMS(){
			var postUrl = '${ctx}/wechat/headhunter/hunter/sendSecurityCode.do';
			var param = {};
			var phone = $("#phone").val();
			param['phone'] = phone;
			var that = this;
			$.ajax({
				url : postUrl,
				type : "GET",
				data : param,
				success : function(data) {
					alert(data.msg);

				},
				error : function() {
					alert("服务出错，请稍后尝试");
					$(that).removeClass('disabled');
				}
			});
		}
		
		function countDown(dom){
	        var sTime = 60,     //倒计时的时间
	            timer = null;

	        clearTimeout(timer);
	        timer = setTimeout(function () {
	            if(sTime == 0){
	            	 dom.removeAttribute('disabled');
	            	 dom.innerHTML = "发送";
	                 clearTimeout(timer);
	                 return false;
	            }else{
	                sTime--;
	                setTimeout(arguments.callee, 1000);
	            }
	            dom.innerHTML = sTime+"s";
	        },1000);
	    }
		
		var handler = {
			activate : function() {
				$(this).addClass('active').siblings().removeClass('active');

				if ($(this).attr('id') == 'closeit') {
					wx.closeWindow();
				}
				
				if ($(this).attr('id') == 'next') {

					var phone = $("#phone").val();
					var code = $("#code").val();
					var hrid = $("#hrid").val();
					if (!code || code == "") {
						alert("您必须输入验证码");
						return false;
					}

					if (!phone || phone == "") {
						alert("您必须输入手机号码");
						return false;
					}
					var regEx = /^1[0-9]{2}[0-9]{8}$/;
					if (!phone.match(regEx)) {
						alert("您必须输入合格的手机号码");
						return false;
					}
					$(this).addClass('disabled');
					var that = this;

					var postUrl = '${ctx}/wechat/hunter/bindePhone.do';
					var param = {};
					param['secureCode'] = code;
					param['phone'] = phone;
					if (hrid) {
						param['id'] = hrid;
					}

					$.ajax({
							url : postUrl,
							type : "POST",
							data : param,
							success : function(data) {
								if (data.errorcode == "0") {
									var hunter = data.data;
									location.href = '${ctx}/wechat/hunter/toEdit.page?hunterId='
											+ hunter.id;
									return;
								} else if (data.errorcode == "1") {
									alert(data.msg);
									//  location.href = '${ctx}/wechat/employer/main.page';
									return;
								} else {
									alert(data.msg);
									$(that).removeClass('disabled');
								}
							},
							error : function() {
								alert("服务出错，请稍后尝试");
								$(that).removeClass('disabled');
							}
						});
				}
			}
		};

		$(document).ready(function() {

			$('.button').on('click', handler.activate);
			// bind events to buttons
			$(' .checkbox').checkbox().first().checkbox({
				onChecked : function() {
					$('#next').removeClass('disabled');
				},
				onUnchecked : function() {
					$('#next').addClass('disabled');
				}
			});
			wx.ready(function() {
				wx.hideOptionMenu();
			});
		});
	</script>
</body>
</html>